<?php

class controller_links extends __registry_controller
{
    public $_ = array();
    public $_layout = NULL;

    public $_content_top = NULL;
    public $_content_bot = NULL;
    public $_content_right = NULL;
    public $_content_left = NULL;
    public $_content_header = 'OPEN';
    public $_content_footer = NULL;
    public $limit = 3;

    public function __construct()
    {	
		$this->_layout =NULL;
        parent::add_object(array(
            'model_links',
            'handle_paging',
            'handle_upload',
            'handle_images'
        ));
	}
	
	public function index_action($cid = null)
	{
	   /* Check permission */
		$this->per_check_permission();
		
       $seo2 = isset($this->_seokey[2])?$this->_seokey[2]:null;
       $this->_['com'] = $this->_seokey[1];

       /*****************pagination**********************/
       $limit = $this->limit;
       $current = end($this->_seokey)?intval(end($this->_seokey)):1;
       $total = $this->mdl_get_total('links');
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/'.$seo2:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
       
       $this->_['url_add'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('add');
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("index");
       $arrHtml = $this->view->view_get_block_html($html);

       $list_item = $this->mdl_get_list_item($current,$limit);
       if(!empty($list_item)){
            foreach($list_item as $item){
                $check_img = preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $item['Picture']);
                if($check_img)
                {
                    $item['img'] = $item['Picture'];
                }else
                {
                    $path_images = PATH_BASE . 'upload/images/links/' .$item['Picture'];
                    if(!empty($item['Picture']) && file_exists($path_images))
                    {
                        $file_thumb = $this->_image_resize($path_images,135,135);
                        $item['img'] = URL_BASE . 'upload/images/links/' .$file_thumb;
                    }else
                    {
                        $item['img'] = URL_BASE;
                    }
                    
                    
                }
                $strOkHtml .= $this->view->view_assign($arrHtml['category'],$item);
            
            }
       }
       $strHtmlContent = $this->view->view_assign_block($html, 
                                                        array(
                                                            'htmlCat'=>$strOkHtml
                                                        )
       );
	   return $strHtmlContent;
	}

    public function add_action(){
		
		/* Check permission */
		$this->per_check_permission();
		
        $this->_['label-error'] = '';
        $this->_layout = 'iframe';
        $this->_['com'] = 'links';
        $this->_['error'] = '';
        if(isset($_POST) && !empty($_POST)){
            $title_link = isset($_POST['title']) && $_POST['title'] != ''?$_POST['title']:'';
            $link = isset($_POST['link']) && $_POST['link'] != ''?$_POST['link']:'';
            $file_website = isset($_POST['file_website']) && $_POST['file_website'] != ''?$_POST['file_website']:'';
            $order = isset($_POST['order']) && $_POST['order'] != ''?$_POST['order']:1;
            $type = isset($_POST['type']) && $_POST['type'] != ''?$_POST['type']:'';
            $option = isset($_POST['option']) && $_POST['option'] != ''?$_POST['option']:'';
            $status = $_POST['Status'];

            if($title_link != '' && $type != '')
            {
                $data= array(
                    'Title'=>$title_link,
                    'Type'=>$type,
                    'Order'=>(int)$order,
                    'Status'=>$status,
                    'Link'=>$link
                );
                if($option == 'local')
                {
                    /* Upload pic */
                    if(isset($_FILES['upload_file']) && !empty($_FILES['upload_file']['name']))
                    {
                        $img = $this->upload_img($_FILES['upload_file'],PATH_BASE.'upload/images/links/',mktime());
                        if($img)
                        {
                            $_POST['Img'] = $img;
                        }
                        else{
                            $this->_['sms'] = $this->_['img_not_format'];
                        }
                    }
                    $data['Picture'] = $_POST['Img'];
                }else{
                    $check_url = preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $file_website);
                    if(!$check_url)
                    {
                        $file_website = URL_BASE . $file_website;
                    }
                    $data['Picture'] = $file_website;
                }
                $result = $this->mdl_save_data('links', $data);
                $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');     
            }else
            {
                $this->_['error'] = $this->_['error_label'];   
            }
            
        }
        /******************************list type**********************/
        $position_array = array('content_header','content_left','content_top','content_mid','content_bot','content_right','content_footer');
        $strHtml = $this->view->view_display('add');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strOkHtml = '';
        foreach($position_array as $key=>$value)
        {
            $strOkHtml .= $this->view->view_assign($arrHtml['position'],
                                                    array(
                                                            'type_name'           => $value,
                                                            'selected_type'       => ''
                                                    )
            );
        }
        
        $strHtmlContent = $this->view->view_assign_block($strHtml, array('list_position_name'=>$strOkHtml));
        return $strHtmlContent;
    }
    
    public function edit_action(){
	
		/* Check permission */
		$this->per_check_permission();
		
        $this->_['label-error'] = '';
        $this->_layout = 'iframe';
        $this->_['com'] = 'links';
        $this->_['error'] = '';
        $id = $this->_seokey[3];

        if(isset($_POST) && !empty($_POST)){
            $title_link = isset($_POST['title']) && $_POST['title'] != ''?$_POST['title']:'';
            $link = isset($_POST['link']) && $_POST['link'] != ''?$_POST['link']:'';
            $file_website = isset($_POST['file_website']) && $_POST['file_website'] != ''?$_POST['file_website']:'';
            $order = isset($_POST['order']) && $_POST['order'] != ''?$_POST['order']:1;
            $type = isset($_POST['type']) && $_POST['type'] != ''?$_POST['type']:'';
            $option = isset($_POST['option']) && $_POST['option'] != ''?$_POST['option']:'';
            $status = $_POST['Status'];

            if($title_link != '' && $type != '')
            {
                $data= array(
                    'Title'=>$title_link,
                    'Type'=>$type,
                    'Order'=>(int)$order,
                    'Status'=>$status,
                    'Link'=>$link
                );
                
                
                if($option == 'local')
                {
                    /* Upload pic */
                    if(isset($_FILES['upload_file']) && !empty($_FILES['upload_file']['name']))
                    {
                        $img = $this->upload_img($_FILES['upload_file'],PATH_BASE.'upload/images/links/',mktime());
                        if($img)
                        {
                            $_POST['Img'] = $img;
                        }
                        else{
                            $this->_['sms'] = $this->_['img_not_format'];
                        }
                        $data['Picture'] = $_POST['Img'];
                    }
                }else{
                    $check_url = preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $file_website);
                    if(!$check_url)
                    {
                        $file_website = URL_BASE . $file_website;
                    }
                    $data['Picture'] = $file_website;
                }
                $result = $this->model_update_data('links', $data, " ID = $id");
                $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');     
            }else
            {
                $this->_['error'] = $this->_['error_label'];   
            }
            
        }
        
        $item = $this->mdl_get_item('links',$id);
        if(empty($item))
        {
            $this->request->rq_goto_window_parent(URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/');    
        }
        
        $check_img = preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $item['Picture']);
        $source_local = ''; $source_web = '';
        if($check_img)
        {
            $item['img'] = $item['Picture'];
            $source_local = '';
            $source_web = 'checked=""';
        }else
        {
            $item['img'] = URL_BASE . '/upload/images/links/' .$item['Picture'];
            $source_local = 'checked=""';
            $source_web = '';
        }
 
        $item['source_local'] = $source_local;
        $item['source_web'] = $source_web;
        /******************************list tytpe**********************/
        $position_array = array('content_header','content_left','content_top','content_mid','content_bot','content_right','content_footer');
        $strHtml = $this->view->view_display('edit');
        $arrHtml = $this->view->view_get_block_html($strHtml);
        $strOkHtml = '';
        foreach($position_array as $key=>$value)
        {
            $selected_type = '';
            if($item['Type'] == $value)
                $selected_type = 'selected=""';
            $strOkHtml .= $this->view->view_assign($arrHtml['position'],
                                                    array(
                                                            'type_name'           => $value,
                                                            'selected_type'       => $selected_type
                                                    )
            );
        }
        
        $strHtmlContent = $this->view->view_assign_block($strHtml, array('list_position_name'=>$strOkHtml));
        $strHtmlContent = $this->view->view_assign($strHtmlContent, $item);
        return $strHtmlContent;
    }
    
    public function index_ajax_action(){
	
		/* Check permission */
		$this->per_check_permission();
		
        $this->_layout = 'NO_LAYOUT';
        $action = isset($this->_seokey[3])?$this->_seokey[3]:null;
        $str_id = isset($this->_seokey[4])?$this->_seokey[4]:null;
        $str_path = isset($this->_seokey[5])?$this->_seokey[5]:null;
        $html = '';
        if(isset($action)){
            switch($action)
            {
               case "updateStatus":
                   $html = $this->updateStatus();
                   break;
               case "filter":
                   $html = $this->filter($str_id);
                   break;
               case "hide":
                   $done = $this->mdl_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               case "show":
                   $done = $this->mdl_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               case "delete":
                   $done = $this->mdl_tool_func($this->model,$action,$str_id);
                    if($done){
                        return 'ok';
                    }else{
                        return 'noOk';
                    }
                   break;
               default:
            }
            return $html;    
        }
    }
    
    public function getAction()
    {
        if(isset($_POST['id_seokey']) && !empty($_POST['id_seokey'])){
            $id_seokey = (int)$_POST['id_seokey'];
            $get_action_from_seokey = $this->mdp_get_action_from_seokey($id_seokey);
            $list_action = $this->mdp_get_seokey_action();
            $action_array = array();
            if(!empty($list_action))
            {
                foreach($list_action as $action)
                {
                    $action_array[$action['ActName']] = $action['SeoKey'];
                }
            }
            if(!empty($get_action_from_seokey))
            {
                $html = '<select name="action" class="field select small" tabindex="3">';
                foreach($get_action_from_seokey as $value)
                {
                    $html .= '<option value="'.$value['ActName'].'">'.$action_array[$value['ActName']].'</option>';
                }
                $html .= '</select>';
            }else
            {
                $html = 'no-data';
            }
        }else
        {
            $html =  'no-data';
        }
        
        return $html;
    }
    
    public function getSeokey()
    {
        if(isset($_POST['app'])){
            $app = $_POST['app'];
            $list_seokey_from_app = $this->mdp_get_seokey_from_app($app);
            if(!empty($list_seokey_from_app))
            {
                $html = '<select id="seo_key" name="seo_key" class="field select small" tabindex="3">';
                foreach($list_seokey_from_app as $value)
                {
                    $html .= '<option  title="'.$value['CatID'].'" value="'.$value['CatID'].'">'.$value['SeoKey'].'</option>';
                }
                $html .= '</select>';
            }else
            {
                $html = 'no-data';
            }
        }else
        {
            $html =  'no-data';
        }
        
        return $html;
    }
    
    public function filter($cid)
    {
       $seo2 = isset($this->_seokey[2])?$this->_seokey[2]:null;
       $this->_['com'] = $this->_seokey[1];
       
       /*****************pagination**********************/
       $limit = $this->limit;
       $current = 1;
       $total = $this->mdp_get_total('permission', $cid);
       $url = !empty($cid)?URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1].'/'.$seo2:URL_BASE.$this->_seokey[0].'/'.$this->_seokey[1];
       $this->_['paging'] = $this->paging_display($total, $current, $limit, $url, $this->_,'/','.htm');
       
       $this->_['url_edit'] = URL_BASE.'admin/'.$this->_seokey[1].'/'.$this->route->rt_get_seokey_act('edit');
	   
       $strHtmlContent = '';
       $strOkHtml = '';
       $html = $this->view->view_display("ajax_filter");
       $arrHtml = $this->view->view_get_block_html($html);

       $list_permission = $this->mdp_get_list_permission($current,$limit,$cid);
       
       $list_action = $this->mdp_get_seokey_action();
       $action_array = array();
       if(!empty($list_action))
       {
            foreach($list_action as $action)
            {
                $action_array[$action['ActName']] = $action['Name'];
            }
       }
       if(!empty($list_permission)){
            foreach($list_permission as $permission){
                $permission['Action'] = $action_array[$permission['Action']];
                $strOkHtml .= $this->view->view_assign($arrHtml['category'],$permission);
            
            }
       }
       
       
       
       $strHtmlContent = $this->view->view_assign_block($html, 
                                                        array(
                                                            'htmlCat'=>$strOkHtml
                                                        )
       );
	   return $strHtmlContent;
    }
    
    
}